from rest_framework.viewsets import ModelViewSet
from goods.models import SKU
from meiduo_admin.serializers.skuSerializer import SKUSerializer, CategoriesSerializer
from meiduo_admin.utils import PaginatorShow
from goods.models import GoodsCategory
from meiduo_admin.serializers.skuSerializer import SPUSimpleSerializer, SPUSepecificationSerializer
from goods.models import SPU, SPUSpecification
from meiduo_admin.permissions import *


class SKUView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    permission_classes = [IsAdminUser]

    pagination_class = PaginatorShow

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return self.queryset.filter(name__contains=keyword)
        return self.queryset.all()


class CategoriesView(ModelViewSet):
    """获取三级分类"""
    queryset = GoodsCategory.objects.all()
    serializer_class = CategoriesSerializer
    permission_classes = [IsAdminUser]

    def get_queryset(self):

        return self.queryset.filter(parent_id__gt=37)


class SPUSimpleView(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer
    permission_classes = [IsAdminUser]


class SPUSpecsView(ModelViewSet):
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSepecificationSerializer
    permission_classes = [IsAdminUser]

    def get_queryset(self):
        pk = self.kwargs.get("pk")
        return self.queryset.filter(spu_id=pk)
